package edu.task.repository;

import edu.task.model.SysExplain;
import edu.task.model.SysRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysExplainRepository extends JpaRepository<SysExplain, Long> {

    @Modifying
    @Query(value = " delete ue from sys_user_explain as ue " +
            "inner join sys_user as u on ue.user_id = u.user_id " +
            "inner join sys_explain as e on ue.explain_id = e.explain_id " +
            "where ue.explain_id=:explainId ", nativeQuery = true)
    void deleteUserExplainByExplainId(Long explainId);

    @Modifying
    @Query(value = " delete from sys_explain where explain_id=:explainId ", nativeQuery = true)
    int deleteExplainById(Long explainId);

    @Query(value = "select * from sys_explain as e " +
            "inner join sys_user_explain as ue on ue.explain_id = e.explain_id " +
            "inner join sys_user as u on u.user_id = ue.user_id " +
            "where ue.user_id=:userId", nativeQuery = true)
    List<SysExplain> selectExplainListByUserId(Long userId);


}
